package com.gzxw.mogublog.xo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gzxw.mogublog.commons.entity.Blog;
import com.gzxw.mogublog.commons.entity.Comment;
import com.gzxw.mogublog.commons.entity.User;
import com.gzxw.mogublog.utils.StringUtils;
import com.gzxw.mogublog.xo.mapper.CommentMapper;
import com.gzxw.mogublog.xo.service.BlogService;
import com.gzxw.mogublog.xo.service.CommentService;
import com.gzxw.mogublog.xo.service.UserService;
import com.gzxw.mogublog.xo.vo.CommentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Autowired
    private UserService userService;

    @Autowired
    private BlogService blogService;

    @Override
    public IPage<Comment> getPageList(CommentVO commentVO) {

        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(commentVO.getKeyword()) && !StringUtils.isEmpty(commentVO.getKeyword().trim())) {
            queryWrapper.like("content", commentVO.getKeyword().trim());
        }

        if (commentVO.getType() != null) {
            queryWrapper.eq("TYPE", commentVO.getType());
        }

        if (StringUtils.isNotEmpty(commentVO.getSource())) {
            queryWrapper.eq("source", commentVO.getSource());
        }

        if (StringUtils.isNotEmpty(commentVO.getUserName())) {
            String userName = commentVO.getUserName();
            QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
            userQueryWrapper.like("nick_name", userName);
            userQueryWrapper.eq("status", 1);
            List<User> list = userService.list(userQueryWrapper);
            if (list.size() > 0) {
                List<String> userUid = new ArrayList<>();
                list.forEach(item -> {
                    userUid.add(item.getUid());
                });
                queryWrapper.in("user_uid", userUid);
            } else {
                // 当没有查询到用户时，默认UID
                queryWrapper.in("user_uid", "");
            }
        }

        // 分页
        Page<Comment> page = new Page<>();
        page.setCurrent(commentVO.getCurrentPage());
        page.setSize(commentVO.getPageSize());
        queryWrapper.eq("status", 1);
        queryWrapper.orderByDesc("create_time");

        IPage<Comment> pageList = this.page(page, queryWrapper);
        List<Comment> commentList = pageList.getRecords();
        Set<String> userUidSet = new HashSet<>(); // 保存所有的user_uid
        Set<String> blogUidSet = new HashSet<>(); // 保存所有的博客uid

        commentList.forEach(item -> {
            if (StringUtils.isNotEmpty(item.getUserUid())) {
                userUidSet.add(item.getUserUid());
            }
            if (StringUtils.isNotEmpty(item.getToUserUid())) {
                userUidSet.add(item.getToUserUid());
            }
            if (StringUtils.isNotEmpty(item.getBlogUid())) {
                blogUidSet.add(item.getBlogUid());
            }
        });

        // 获取博客
        Collection<Blog> blogList = new ArrayList<>();
        if (blogUidSet.size() > 0) {
            blogList = blogService.listByIds(blogUidSet);
        }
        Map<String, Blog> blogMap = new HashMap<>();
        blogList.forEach(item -> {
            // 评论管理并不需要查看博客内容，因此将其排除
            item.setContent("");
            blogMap.put(item.getUid(), item);
        });

        Collection<User> userCollection = new ArrayList<>();
        if (userUidSet.size() > 0) {
            userCollection = userService.listByIds(userUidSet);
        }

        Map<String, User> userMap = new HashMap<>();
        userCollection.forEach(item -> {
            userMap.put(item.getUid(), item);
        });

        for (Comment item : commentList) {

            if (StringUtils.isNotEmpty(item.getUserUid())) {
                item.setUser(userMap.get(item.getUserUid()));
            }
            if (StringUtils.isNotEmpty(item.getToUserUid())) {
                item.setToUser(userMap.get(item.getToUserUid()));
            }
            if (StringUtils.isNotEmpty(item.getBlogUid())) {
                item.setBlog(blogMap.get(item.getBlogUid()));
            }
        }
        pageList.setRecords(commentList);
        return pageList;
    }

    @Override
    public String addComment(CommentVO commentVO) {
        Comment comment = new Comment();
        comment.setSource(commentVO.getSource());
        comment.setBlogUid(commentVO.getBlogUid());
        comment.setContent(commentVO.getContent());
        comment.setUserUid(commentVO.getUserUid());
        comment.setToUid(commentVO.getToUid());
        comment.setToUserUid(commentVO.getToUserUid());
        comment.setStatus(1);
        comment.setUpdateTime(new Date());
        comment.insert();
        return "插入成功";
    }

    @Override
    public String editComment(CommentVO commentVO) {
        Comment comment = this.getById(commentVO.getUid());
        comment.setSource(commentVO.getSource());
        comment.setBlogUid(commentVO.getBlogUid());
        comment.setContent(commentVO.getContent());
        comment.setUserUid(commentVO.getUserUid());
        comment.setToUid(commentVO.getToUid());
        comment.setToUserUid(commentVO.getToUserUid());
        comment.setStatus(1);
        comment.setUpdateTime(new Date());
        comment.updateById();
        return "编辑评论";
    }

    @Override
    public String deleteComment(CommentVO commentVO) {
        Comment comment = this.getById(commentVO.getUid());
        comment.setStatus(1);
        comment.setUpdateTime(new Date());
        comment.updateById();
        return "删除成功";
    }

    @Override
    public String deleteBatchComment(List<CommentVO> commentVoList) {
        if (commentVoList.size()<0) {
            return "传入的参数有误";
        }
        List<String> uids = new ArrayList<>();
        commentVoList.forEach(item -> {
            uids.add(item.getUid());
        });

        Collection<Comment> commentList = this.listByIds(uids);

        commentList.forEach(item -> {
            item.setUpdateTime(new Date());
            item.setStatus(0);
        });

        this.updateBatchById(commentList);

        return "删除成功";
    }
}
